package com.nf.health.app.homepage.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.global.WebViewActivity;
import com.nf.health.app.adapter.ViewHolder;
import com.nf.health.app.models.DetectionResultExtra;
import com.nf.health.app.models.DetectionResultItem;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ImageLoaderUtils;
import com.nf.health.app.utils.ViewUtil;

import java.util.List;

/**
 * 中医体检——体检结果
 * 
 * @author jiax
 * 
 */
public class DetectionResultActivity extends BaseActivity {

	private TextView tv_order_num;
	private TextView tv_project_name;
	private TextView tv_hospital;
	private TextView tv_username;
	private TextView tv_cardnum;
	private TextView tv_detection_result;
	private TextView tv_analiysis;
	private TextView tv_advice1;
	private TextView tv_advice2;
	private ListView lv_improve;
	public static final int TYPE_DETECTION = 1;
	public static final int TYPE_PING_GU = 2;// 评估
	private View ll_doctor_detection;
	private int type;
	private ListView lv_pinggu;
	private List<DetectionResultItem> list;
	private View ll_pinggu;
	private TextView tv_waiting;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		type = getIntent().getIntExtra("type", 1);
		setTitleBar(R.layout.titlebar_base);
		String majorid = getIntent().getStringExtra("majorid");
		setContent(R.layout.activity_detection_result);

		initView();

		provider.requestDetectionResult(majorid, "detail");
	}

	private void initView() {
		tv_order_num = ViewUtil.getView(this, R.id.tv_order_num);
		tv_project_name = ViewUtil.getView(this, R.id.tv_project_name);
		tv_hospital = ViewUtil.getView(this, R.id.tv_hospital);
		tv_username = ViewUtil.getView(this, R.id.tv_username);
		tv_cardnum = ViewUtil.getView(this, R.id.tv_cardnum);
		tv_waiting = ViewUtil.getView(this, R.id.tv_waiting);
		
		switch (type) {
		case TYPE_DETECTION:
			ViewUtil.setText(this, R.id.titlebar_title_tv, "体检结果");
			ll_doctor_detection = ViewUtil.getView(this,R.id.ll_doctor_detection);
			

			tv_detection_result = ViewUtil.getView(this,
					R.id.tv_detection_result);
			tv_analiysis = ViewUtil.getView(this, R.id.tv_analiysis);
			tv_advice1 = ViewUtil.getView(this, R.id.tv_advice1);

			lv_improve = ViewUtil.getView(this, R.id.lv_improve);
			break;
		case TYPE_PING_GU:
			ViewUtil.setText(this, R.id.titlebar_title_tv, "风险评估结果");
			lv_pinggu = ViewUtil.getView(this, R.id.lv_pinggu);
			ll_pinggu = ViewUtil.getView(this, R.id.ll_pinggu);
			

			tv_advice2 = ViewUtil.getView(this, R.id.tv_advice2);

			lv_pinggu.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					if (type == TYPE_PING_GU) {
						Bundle bundle = new Bundle();
						bundle.putString(WebViewActivity.KEY_URL, list.get(position).getUrl());
						ActivityUtils.showActivity(DetectionResultActivity.this, WebViewActivity.class, bundle);
					}
				}
			});

			break;
		default:
			break;
		}
	}

	@Override
	public void handleActionSuccess(String actionName, Object object) {
		if ("detail".equals(actionName)) {
			DetectionResultExtra extra = (DetectionResultExtra) object;
			tv_order_num.setText("订单号：" + extra.getUserinfo().getOrderno());
			tv_project_name.setText("检测名称："
					+ extra.getUserinfo().getRisk_evaluation());
			if (TextUtils.isEmpty(extra.getUserinfo().getDoctorname())) {
				tv_hospital
						.setText("医院医生：" + extra.getUserinfo().getHospital());
			} else {
				tv_hospital.setText("医院医生：" + extra.getUserinfo().getHospital()
						+ "(" + extra.getUserinfo().getDoctorname() + ")");
			}
			tv_username.setText("姓名：" + extra.getUserinfo().getUsername());
			tv_cardnum.setText("卡号：" + extra.getUserinfo().getCardno());
			try {
				//extra.getData()可能为null
				
				switch (type) {
				case TYPE_DETECTION:
					tv_detection_result.setText("您的检测结果为："+ extra.getData().getAnaliysis().getName());
					tv_analiysis.setText(extra.getData().getAnaliysis().getAnalysis());
					lv_improve.setAdapter(new MyImproveAdapter(extra.getData().getImprove()));
					tv_advice1.setText(extra.getData().getAdvice());
					ll_doctor_detection.setVisibility(View.VISIBLE);
					break;
				case TYPE_PING_GU:
					list = extra.getData().getList();
					lv_pinggu.setAdapter(new MyImproveAdapter(list));
					if(TextUtils.isEmpty(extra.getData().getAdvice())){
						if(ll_pinggu != null){
							ll_pinggu.setVisibility(View.GONE);
						}
						tv_waiting.setVisibility(View.VISIBLE);
					}else{
						tv_advice2.setText(extra.getData().getAdvice());
						ll_pinggu.setVisibility(View.VISIBLE);
					}
					break;
				default:
					break;
				}
			} catch (Exception e) {
				if(ll_doctor_detection !=null){
					ll_doctor_detection.setVisibility(View.GONE);
				}
				if(ll_pinggu != null){
					ll_pinggu.setVisibility(View.GONE);
				}
				tv_waiting.setVisibility(View.VISIBLE);
			}
		}
	}

	class MyImproveAdapter extends BaseAdapter {
		private List<DetectionResultItem> items;

		public MyImproveAdapter(List<DetectionResultItem> improve) {
			this.items = improve;
		}

		@Override
		public int getCount() {
			return items == null ? 0 : items.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = ViewHolder.get(parent, convertView,
					R.layout.item_detection_result_improve, position);
			ImageView iv_icon = holder.getView(R.id.iv_icon);
			ImageView iv_arrow = holder.getView(R.id.iv_arrow);
			switch (type) {
			case TYPE_DETECTION:
				ImageLoaderUtils.getinstance(DetectionResultActivity.this).getImage(iv_icon, items.get(position).getImg());
				holder.setText(R.id.tv_content, items.get(position).getName()
						+ ":" + items.get(position).getContent());
				iv_arrow.setVisibility(View.GONE);
				break;
			case TYPE_PING_GU:
				ImageLoaderUtils.getinstance(DetectionResultActivity.this)
						.getImage(iv_icon, items.get(position).getImg());
				holder.setText(R.id.tv_content, items.get(position)
						.getName());
				break;
			default:
				break;
			}
			return holder.getConvertView();
		}

	}
}
